package com.example.librarysystem.repository;

import com.example.librarysystem.entity.BorrowRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Repository;

import java.sql.Types;
import java.util.List;
import java.util.Map;

@Repository
public class BorrowRecordRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public String borrowBook(String borrowId, String isbn, String readerId) {
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withProcedureName("sp_BorrowBook")
                .withoutProcedureColumnMetaDataAccess()
                .declareParameters(
                        new SqlParameter("p_BorrowID", Types.VARCHAR),
                        new SqlParameter("p_ISBN", Types.VARCHAR),
                        new SqlParameter("p_ReaderID", Types.VARCHAR))
                .returningResultSet("Message", (rs, rowNum) -> rs.getString("Message"));

        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("p_BorrowID", borrowId)
                .addValue("p_ISBN", isbn)
                .addValue("p_ReaderID", readerId);

        Map<String, Object> result = jdbcCall.execute(in);
        List<String> messages = (List<String>) result.get("Message");
        return messages != null && !messages.isEmpty() ? messages.get(0) : "操作失败";
    }

    public String returnBook(String borrowId) {
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withProcedureName("sp_ReturnBook")
                .withoutProcedureColumnMetaDataAccess()
                .declareParameters(
                        new SqlParameter("p_BorrowID", Types.VARCHAR))
                .returningResultSet("Message", (rs, rowNum) -> rs.getString("Message"));

        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("p_BorrowID", borrowId);

        Map<String, Object> result = jdbcCall.execute(in);
        List<String> messages = (List<String>) result.get("Message");
        return messages != null && !messages.isEmpty() ? messages.get(0) : "操作失败";
    }

    public List<BorrowRecord> getBorrowRecords(String readerId) {
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withProcedureName("sp_GetReaderBorrows")
                .withoutProcedureColumnMetaDataAccess()
                .declareParameters(
                        new SqlParameter("p_ReaderID", Types.VARCHAR))
                .returningResultSet("records", new BeanPropertyRowMapper<>(BorrowRecord.class));

        SqlParameterSource in = new MapSqlParameterSource()
                .addValue("p_ReaderID", readerId);

        Map<String, Object> result = jdbcCall.execute(in);
        return (List<BorrowRecord>) result.get("records");
    }
}